// pages/home/index.js
import { Product } from '../../model/product.js';
import { User } from '../../model/user.js';
var user = new User()
var product = new Product();
var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    productList: [],
	storelist:[],
	showNameStatus:false,
	showstorename:'全部',
    active: 0,
    icon: {
      home: '../../utils/image/home.png',
      home_active: '../../utils/image/home-active.png',
      order: '../../utils/image/order.png',
      order_active: '../../utils/image/order-active.png',
      user: '../../utils/image/user.png',
      user_active: '../../utils/image/user-active.png'
    }
  },

  /**
   * 切换菜单
   */
  onChangeTabbar(e){
    if (e.detail == 0) {
      if (appInst.globalData.userType != 1) {
        appInst.goHome()
      } else {
        if (appInst.globalData.order) {
          appInst.goHome()
        }
      }
    }else if(e.detail == 1) {
      wx.switchTab({
        url: '../order/order',
      })
    }else  if(e.detail == 2) {
      wx.switchTab({
        url: '../user/user',
      })
    }
  },
  onClickHide() {
    this.setData({ showNameStatus: false });
  },
  onConfirmOne(event) {
    
  },
  onClickShowOne() {
    this.setData({ showNameStatus: true });
  },
  onConfirmOne(event) {
		this.setData({
			showstorename:event.detail.value
		})
		if(event.detail.value=='全部'){
			this.setData({
				productList:appInst.globalData.appproductList
			})
		}else{
			var tempprolist = [];
			appInst.globalData.appproductList.forEach(function(item){
				if(item.store.name==event.detail.value)
					tempprolist.push(item);
			})
			this.setData({
				productList:tempprolist
			})
		}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar();
    const scene = decodeURIComponent(options.scene)
    if (scene && scene != 'undefined'){
      this.createInvite(scene)
    }else{
      this.getProduct();
    }
  },
  onShow:function(){
      if(appInst.globalData.showstoreid!=null)
        this.getstore(appInst.globalData.showstoreid);
      if(appInst.globalData.appstorename!='美业团')
        appInst.settitle();
  },

  createInvite(scene){
     user.createInvite({scene:scene},(res)=>{
       this.getProduct();
     })
  },

  /**
   * 获取产品列表
   */
  getProduct() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    product.getProductList((result)=>{
      this.setData({
        productList: result,
      });
	  var storel = ['全部'];
	  that.data.productList.forEach(function(item){
		  if(storel.indexOf(item.store.name)==-1)
			storel.push(item.store.name);
	  })
	  that.setData({
		  storelist:storel
	  })
      appInst.globalData.appproductList = result;
      this.getUserInfo();
      wx.hideLoading();
    })
  },


  /**
   * 获取本店铺的详情
   */
  getstore(id){
      var showtempproductlist = [];
      var list = appInst.globalData.appproductList;
      
      list.forEach(function(item){
        if(item.store_id==appInst.globalData.showstoreid)
          showtempproductlist.push(item);
      })
      this.setData({
        productList:showtempproductlist
      })
  },

  getUserInfo(){
    user.getUserInfo((res)=>{
      if(res.store)
        appInst.globalData.appstorename  = res.store.name
      
      appInst.globalData.userType = res.type
      appInst.globalData.order = res.order
      if (appInst.globalData.userType == 1) {
        if (appInst.globalData.order) {
          this.setData({
            hidden: true,
            active: 3,
          });
        }else {
          this.setData({
            hidden: true,
          });
        }
      } else {
        this.setData({
          hidden: true,
          active: 3,
        });
      }
      wx.hideLoading();
      if (appInst.globalData.timeNum == 1) {
        appInst.globalData.timeNum = 2
        appInst.goHome()
      }
    })
  },

   /**
   * 打开新页面
   */
  openWin(e) {
    wx.navigateTo({
      url: `/pages/confirm/confirm?id=${e.currentTarget.dataset.id}&team_user_id=0`,
    })
  },
  /**
   * 详情页面
   */
  openProdut(e) {
    wx.navigateTo({
      url: `/pages/product/product?id=${e.currentTarget.dataset.id}`,
    })
  }
})
